<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="./layui/css/layui.css">
  <title>表单验证</title>
</head>

<body>


  <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>表单验证</legend>
  </fieldset>

  <!-- required（必填项）  -->
  <!-- phone（手机号） email（邮箱） url（网址） number（数字） date（日期） identity（身份证） 必填项  -->
  
  <div class="layui-form">
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">验证手机</label>
        <div class="layui-input-inline">
          <input type="tel" lay-verify="phone" name="phone" id="Phone" autocomplete="off" value="13418883867" class="layui-input">
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">验证邮箱</label>
        <div class="layui-input-inline">
          <input type="text" lay-verify="email" name="email" autocomplete="off" class="layui-input">
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">验证日期</label>
        <div class="layui-input-inline">
          <input type="text" id="date" lay-verify="date" name="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">验证链接</label>
        <div class="layui-input-inline">
          <input type="tel" lay-verify="url" name="url" autocomplete="off" class="layui-input">
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">验证数字</label>
        <div class="layui-input-inline">
          <input type="tel" lay-verify="number" name="number" autocomplete="off" class="layui-input">
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">验证身份证</label>
      <div class="layui-input-block">
        <input type="text" lay-verify="identity" name="identity" placeholder="" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">旧密码</label>
      <div class="layui-input-block">
        <input type="text" lay-verify="oldPwd" name="oldPwd" placeholder="请输入旧密码" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">新密码</label>
      <div class="layui-input-block">
        <input type="text" lay-verify="newPwd" placeholder="请输入6到12位密码" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">确认密码</label>
      <div class="layui-input-block">
        <input type="text" lay-verify="newPwdAgain" placeholder="请输入确认密码" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">支付密码</label>
      <div class="layui-input-block">
        <input type="text" lay-verify="payPwd" placeholder="请输入6位密码" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">确认支付密码</label>
      <div class="layui-input-block">
        <input type="text" lay-verify="payPwdAgain" placeholder="请输入确认支付密码" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">金额</label>
        <div class="layui-input-inline">
          <input type="text" lay-verify="money" autocomplete="off" class="layui-input" id="money">
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">积分</label>
        <div class="layui-input-inline">
          <input type="text" lay-verify="code" autocomplete="off" class="layui-input" id="code">
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label">短信验证码</label>
        <div class="layui-input-inline">
          <input type="text" lay-verify="number" autocomplete="off" class="layui-input" id="SMGCode">
        </div>
        <div class="layui-form-mid layui-word-aux" style="padding:0!important">
          <button class="layui-btn layui-btn-primary" id="GetSMGCode" style="width:119px">获取验证码</button>
        </div>
      </div>
    </div>

    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
      </div>
    </div>
  </div>
  <script src="./layui/layui.js"></script>
  <script src="./js/config.js"></script>
  <script>
    layui.use(['jquery', 'form', 'laydate', 'verify'], function () {
      var $ = layui.$,
        form = layui.form,
        laydate = layui.laydate,
        verify = layui.verify;
      //日期
      laydate.render({
        elem: '#date'
      });

      // 金额
      $('#money').on("input propertychange", function () {
        var _value = verify.changeCurrency({
          value: $(this).val(),
          accuracy: 2
        });
        $(this).val(_value)
      });

      // 积分
      $('#code').on("input propertychange", function () {
        var _value = verify.changeCurrency({
          value: $(this).val(),
          accuracy: 4
        });
        $(this).val(_value)
      });

      //监听提交
      form.on('submit(demo1)', function (data) {
        console.log(data);
        return false;
      });

      

      $("#GetSMGCode").click(function () {
        if (!verify.CheckPhone($("#Phone").val())) return;
        var url ="https://www.easy-mock.com/mock/5aac8b201244b8515e92613b/layui/GetCodeTimeSpan"
        verify.SendCode({
          el: $("#GetSMGCode"),
          url: url,
          phone: $("#Phone").val(),
          fn: function (data) {
            console.log(data)
            if (data) {
              GetCodeTimeSpan();
            }
          }
        });
      });

      // 获取后台验证码倒计时地址 
      GetCodeTimeSpan();
      function GetCodeTimeSpan() {
        var url = 'https://www.easy-mock.com/mock/5aac8b201244b8515e92613b/layui/GetCodeTimeSpan'
        verify.GetCodeTimeSpan({
          url: url,
          fn: function (data) {
            console.log(data)
            verify.SetSMSCheckCodeTimeOut({
              el: $("#GetSMGCode"),
              surplus: data.Data.msg
            });
          }
        });
      }     
    });
  </script>
</body>

</html>